package com.ocom.oauth.auth.ali;

import com.ocom.oauth.auth.CustomAbstractTokenGranter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 *
 *  说明：
 *    验证码
 *    定义验证码具体逻辑
 */
public class AliTokenGranter extends CustomAbstractTokenGranter {

    private static final String CODE_AUTH = "alixcx_login";

    private AliUserDetailService aliUserDetailService;

    public AliTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, AliUserDetailService aliUserDetailService) {
        super(tokenServices, clientDetailsService, requestFactory, CODE_AUTH);
        this.aliUserDetailService = aliUserDetailService;
    }

    @Override
    protected UserDetails getUserDetails(Map<String, String> parameters) {
        String wx_code = parameters.get("code");
        String comId = parameters.get("comId");
        String response = parameters.get("response");
        return aliUserDetailService.loadByCode(wx_code,comId,response);
    }
}

